package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.service.BizService;


@Controller
@RequestMapping(value = "/student")
public class CheckEvalueController extends BaseController {
	
	@Autowired
	private BizService bizService;

	private static Logger logger = LoggerFactory.getLogger(CheckEvalueController.class);
	
	/**
	 * 判断驾校或教练只能评价一次
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/checkStudentEvaluateOne", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String checkStudentEvaluateOne(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			//学员Id
			String studentId = request.getParameter("studentId");
			//1,教练或2,驾校
			String type = request.getParameter("type");
			//教练或驾校的ID
			String objectId = request.getParameter("objectId");
			if((null != studentId && !studentId.isEmpty())
					&& (null != objectId && !objectId.isEmpty())
					&& (null != type && !type.isEmpty())){
				Map<String, Object> valMap = new HashMap<String, Object>();
				valMap.put("studentId", studentId);
				valMap.put("objectId", objectId);
				valMap.put("type", type);
				response =  bizService.checkStudentEvaluateOne(valMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填参数!");
			}
		} catch (Exception e) {
			logger.info("checkStudentEvaluateOne："+ e.toString());
			response.setErrorcode(1);
			response.setMessage("判断失败！");
		} finally {
			return response.toJSON();
		}
	}

	/**
	 * 判断视频只能评价一次
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/checkVideoEvaluateOne", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String checkVideoEvaluateOne(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			//学员Id
			String userId = request.getParameter("userId");
			//视频ID
			String videoId = request.getParameter("videoId");
			if((null != userId && !userId.isEmpty())
					&& (null != videoId && !videoId.isEmpty())){
				Map<String, Object> valMap = new HashMap<String, Object>();
				valMap.put("userId", userId);
				valMap.put("videoId", videoId);
				response =  bizService.checkVideoEvaluateOne(valMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填参数!");
			}
		} catch (Exception e) {
			logger.info("checkVideoEvaluateOne："+ e.toString());
			response.setErrorcode(1);
			response.setMessage("判断失败！");
		} finally {
			return response.toJSON();
		}
	}
	/**
	 * 校验课程只能评价一次且是上过的课程
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/checkCourseEvaluateOne", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String checkCourseEvaluateOne(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			//学员Id
			String studentId = request.getParameter("studentId");
			//课程ID
			String courseId = request.getParameter("courseId");
			if((null != studentId && !studentId.isEmpty())
					&& (null != courseId && !courseId.isEmpty())){
				Map<String, Object> valMap = new HashMap<String, Object>();
				valMap.put("studentId", studentId);
				valMap.put("courseId", courseId);
				response =  bizService.checkCourseEvaluateOne(valMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填参数!");
			}
		} catch (Exception e) {
			logger.info("checkCourseEvaluateOne："+ e.toString());
			response.setErrorcode(1);
			response.setMessage("判断失败！");
		} finally {
			return response.toJSON();
		}
	}
}
